package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class AffixSelectionPane extends AdjustablePanel {
    private AffixRarityPane pnlRarity;
    private DamageSelectionPane pnlDamage;
    private AttribSelectionPane pnlAttrib;
    private LevelSelPane pnlLevel;
    private ItemNamePane itemNamePane;

    public AffixSelectionPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel pnlMain = this.buildMainPanel();
        final JScrollPane scroll = new JScrollPane(pnlMain);
        scroll.getVerticalScrollBar().setUnitIncrement(2 * GDStashFrame.iniConfig.sectUI.fontSize);
        scroll.setWheelScrollingEnabled(true);
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(scroll));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(scroll));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        final Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_AFFIX_INFO"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.pnlRarity == null) {
            this.pnlRarity = new AffixRarityPane(0);
        } else {
            this.pnlRarity.adjustUI();
        }
        if (this.pnlDamage == null) {
            this.pnlDamage = new DamageSelectionPane();
        } else {
            this.pnlDamage.adjustUI();
        }
        if (this.pnlAttrib == null) {
            this.pnlAttrib = new AttribSelectionPane();
        } else {
            this.pnlAttrib.adjustUI();
        }
        if (this.pnlLevel == null) {
            this.pnlLevel = new LevelSelPane();
        } else {
            this.pnlLevel.adjustUI();
        }
        if(this.itemNamePane == null) {
            this.itemNamePane = new ItemNamePane();
        } else {
            this.itemNamePane.adjustUI();
        }
    }

    private JPanel buildMainPanel() {
        final JPanel panel = new JPanel();
        final BorderLayout layout = new BorderLayout();
        final JPanel pnlDmgAttrib = this.buildDamageAttribPanel();
        panel.setLayout(layout);
        panel.add(this.pnlRarity, "North");
        panel.add(pnlDmgAttrib, "Center");
        return panel;
    }

    private JPanel buildDamageAttribPanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel pnlAttrib = this.buildAttribPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlDamage)).addGroup(layout.createParallelGroup().addComponent(pnlAttrib));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlDamage).addComponent(pnlAttrib));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, this.pnlDamage, pnlAttrib);
        return panel;
    }

    private JPanel buildAttribPanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.itemNamePane).addComponent(this.pnlLevel).addComponent(this.pnlAttrib));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.itemNamePane)).addGroup(layout.createParallelGroup().addComponent(this.pnlLevel)).addGroup(layout.createParallelGroup().addComponent(this.pnlAttrib));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.itemNamePane, this.pnlLevel, this.pnlAttrib);
        return panel;
    }

    public void clear() {
        this.pnlRarity.clear();
        this.pnlDamage.clear();
        this.pnlAttrib.clear();
        this.pnlLevel.clear();
        this.itemNamePane.clear();
    }

    /**
     * 添加查询条件
     *
     * @param criteria 查询条件
     * @author pengzhikang
     * @date 2022/07/13 12:02
     */
    public void addCriteria(final SelectionCriteria criteria) {
        this.pnlRarity.addCriteria(criteria);
        this.pnlDamage.addCriteria(criteria);
        this.pnlAttrib.addCriteria(criteria);
        this.pnlLevel.addCriteria(criteria);
        this.itemNamePane.addCriteriaText(criteria);
        criteria.selMode = SelectionCriteria.SelectionMode.AFFIX;
    }
}
